package com.j256.ormlite.android;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import com.j256.ormlite.dao.ObjectCache;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;
import com.j256.ormlite.misc.SqlExceptionUtil;
import com.j256.ormlite.misc.VersionUtils;
import com.j256.ormlite.stmt.GenericRowMapper;
import com.j256.ormlite.stmt.StatementBuilder.StatementType;
import com.j256.ormlite.support.CompiledStatement;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.support.GeneratedKeyHolder;
import java.sql.Savepoint;

public class AndroidDatabaseConnection
  implements DatabaseConnection
{
  private static final String ANDROID_VERSION = "VERSION__4.48__";
  private static final String[] NO_STRING_ARGS;
  private static Logger logger = LoggerFactory.getLogger(AndroidDatabaseConnection.class);
  private final boolean cancelQueriesEnabled;
  private final SQLiteDatabase db;
  private final boolean readWrite;

  static
  {
    NO_STRING_ARGS = new String[0];
    VersionUtils.checkCoreVersusAndroidVersions("VERSION__4.48__");
  }

  public AndroidDatabaseConnection(SQLiteDatabase paramSQLiteDatabase, boolean paramBoolean)
  {
    this(paramSQLiteDatabase, paramBoolean, false);
  }

  public AndroidDatabaseConnection(SQLiteDatabase paramSQLiteDatabase, boolean paramBoolean1, boolean paramBoolean2)
  {
    this.db = paramSQLiteDatabase;
    this.readWrite = paramBoolean1;
    this.cancelQueriesEnabled = paramBoolean2;
    logger.trace("{}: db {} opened, read-write = {}", this, paramSQLiteDatabase, Boolean.valueOf(paramBoolean1));
  }

  private void bindArgs(SQLiteStatement paramSQLiteStatement, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType)
    throws java.sql.SQLException
  {
    if (paramArrayOfObject == null)
      return;
    int i = 0;
    label8: Object localObject;
    if (i < paramArrayOfObject.length)
    {
      localObject = paramArrayOfObject[i];
      if (localObject != null)
        break label40;
      paramSQLiteStatement.bindNull(i + 1);
    }
    label40: SqlType localSqlType;
    while (true)
    {
      i++;
      break label8;
      break;
      localSqlType = paramArrayOfFieldType[i].getSqlType();
      switch (1.$SwitchMap$com$j256$ormlite$field$SqlType[localSqlType.ordinal()])
      {
      default:
        throw new java.sql.SQLException("Unknown sql argument type: " + localSqlType);
      case 1:
      case 2:
      case 3:
        paramSQLiteStatement.bindString(i + 1, localObject.toString());
        break;
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
        paramSQLiteStatement.bindLong(i + 1, ((Number)localObject).longValue());
        break;
      case 9:
      case 10:
        paramSQLiteStatement.bindDouble(i + 1, ((Number)localObject).doubleValue());
        break;
      case 11:
      case 12:
        paramSQLiteStatement.bindBlob(i + 1, (byte[])localObject);
      case 13:
      case 14:
      case 15:
      }
    }
    throw new java.sql.SQLException("Invalid Android type: " + localSqlType);
  }

  private String[] toStrings(Object[] paramArrayOfObject)
  {
    if ((paramArrayOfObject == null) || (paramArrayOfObject.length == 0))
    {
      arrayOfString = null;
      return arrayOfString;
    }
    String[] arrayOfString = new String[paramArrayOfObject.length];
    int i = 0;
    label21: Object localObject;
    if (i < paramArrayOfObject.length)
    {
      localObject = paramArrayOfObject[i];
      if (localObject != null)
        break label47;
      arrayOfString[i] = null;
    }
    while (true)
    {
      i++;
      break label21;
      break;
      label47: arrayOfString[i] = localObject.toString();
    }
  }

  // ERROR //
  private int update(String paramString1, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType, String paramString2)
    throws java.sql.SQLException
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore 5
    //   3: aload_0
    //   4: getfield 48	com/j256/ormlite/android/AndroidDatabaseConnection:db	Landroid/database/sqlite/SQLiteDatabase;
    //   7: aload_1
    //   8: invokevirtual 155	android/database/sqlite/SQLiteDatabase:compileStatement	(Ljava/lang/String;)Landroid/database/sqlite/SQLiteStatement;
    //   11: astore 5
    //   13: aload_0
    //   14: aload 5
    //   16: aload_2
    //   17: aload_3
    //   18: invokespecial 157	com/j256/ormlite/android/AndroidDatabaseConnection:bindArgs	(Landroid/database/sqlite/SQLiteStatement;[Ljava/lang/Object;[Lcom/j256/ormlite/field/FieldType;)V
    //   21: aload 5
    //   23: invokevirtual 160	android/database/sqlite/SQLiteStatement:execute	()V
    //   26: aload 5
    //   28: ifnull +11 -> 39
    //   31: aload 5
    //   33: invokevirtual 163	android/database/sqlite/SQLiteStatement:close	()V
    //   36: aconst_null
    //   37: astore 5
    //   39: aload_0
    //   40: getfield 48	com/j256/ormlite/android/AndroidDatabaseConnection:db	Landroid/database/sqlite/SQLiteDatabase;
    //   43: ldc 165
    //   45: invokevirtual 155	android/database/sqlite/SQLiteDatabase:compileStatement	(Ljava/lang/String;)Landroid/database/sqlite/SQLiteStatement;
    //   48: astore 5
    //   50: aload 5
    //   52: invokevirtual 168	android/database/sqlite/SQLiteStatement:simpleQueryForLong	()J
    //   55: lstore 11
    //   57: lload 11
    //   59: l2i
    //   60: istore 10
    //   62: aload 5
    //   64: ifnull +8 -> 72
    //   67: aload 5
    //   69: invokevirtual 163	android/database/sqlite/SQLiteStatement:close	()V
    //   72: getstatic 29	com/j256/ormlite/android/AndroidDatabaseConnection:logger	Lcom/j256/ormlite/logger/Logger;
    //   75: ldc 170
    //   77: aload 4
    //   79: iload 10
    //   81: invokestatic 175	java/lang/Integer:valueOf	(I)Ljava/lang/Integer;
    //   84: aload_1
    //   85: invokevirtual 66	com/j256/ormlite/logger/Logger:trace	(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    //   88: iload 10
    //   90: ireturn
    //   91: astore 7
    //   93: new 96	java/lang/StringBuilder
    //   96: dup
    //   97: invokespecial 97	java/lang/StringBuilder:<init>	()V
    //   100: ldc 177
    //   102: invokevirtual 103	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   105: aload_1
    //   106: invokevirtual 103	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   109: invokevirtual 110	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   112: aload 7
    //   114: invokestatic 183	com/j256/ormlite/misc/SqlExceptionUtil:create	(Ljava/lang/String;Ljava/lang/Throwable;)Ljava/sql/SQLException;
    //   117: athrow
    //   118: astore 6
    //   120: aload 5
    //   122: ifnull +8 -> 130
    //   125: aload 5
    //   127: invokevirtual 163	android/database/sqlite/SQLiteStatement:close	()V
    //   130: aload 6
    //   132: athrow
    //   133: astore 9
    //   135: iconst_1
    //   136: istore 10
    //   138: aload 5
    //   140: ifnull -68 -> 72
    //   143: aload 5
    //   145: invokevirtual 163	android/database/sqlite/SQLiteStatement:close	()V
    //   148: goto -76 -> 72
    //   151: astore 8
    //   153: aload 5
    //   155: ifnull +8 -> 163
    //   158: aload 5
    //   160: invokevirtual 163	android/database/sqlite/SQLiteStatement:close	()V
    //   163: aload 8
    //   165: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   3	26	91	android/database/SQLException
    //   3	26	118	finally
    //   93	118	118	finally
    //   39	57	133	android/database/SQLException
    //   39	57	151	finally
  }

  public void close()
    throws java.sql.SQLException
  {
    try
    {
      this.db.close();
      logger.trace("{}: db {} closed", this, this.db);
      return;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("problems closing the database connection", localSQLException);
    }
  }

  public void closeQuietly()
  {
    try
    {
      close();
      return;
    }
    catch (java.sql.SQLException localSQLException)
    {
    }
  }

  public void commit(Savepoint paramSavepoint)
    throws java.sql.SQLException
  {
    try
    {
      this.db.setTransactionSuccessful();
      this.db.endTransaction();
      if (paramSavepoint == null)
      {
        logger.trace("{}: transaction is successfuly ended", this);
        return;
      }
      logger.trace("{}: transaction {} is successfuly ended", this, paramSavepoint.getSavepointName());
      return;
    }
    catch (android.database.SQLException localSQLException)
    {
      if (paramSavepoint == null)
        throw SqlExceptionUtil.create("problems commiting transaction", localSQLException);
      throw SqlExceptionUtil.create("problems commiting transaction " + paramSavepoint.getSavepointName(), localSQLException);
    }
  }

  public CompiledStatement compileStatement(String paramString, StatementBuilder.StatementType paramStatementType, FieldType[] paramArrayOfFieldType, int paramInt)
  {
    AndroidCompiledStatement localAndroidCompiledStatement = new AndroidCompiledStatement(paramString, this.db, paramStatementType, this.cancelQueriesEnabled);
    logger.trace("{}: compiled statement got {}: {}", this, localAndroidCompiledStatement, paramString);
    return localAndroidCompiledStatement;
  }

  public int delete(String paramString, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType)
    throws java.sql.SQLException
  {
    return update(paramString, paramArrayOfObject, paramArrayOfFieldType, "deleted");
  }

  public int executeStatement(String paramString, int paramInt)
    throws java.sql.SQLException
  {
    return AndroidCompiledStatement.execSql(this.db, paramString, paramString, NO_STRING_ARGS);
  }

  public int insert(String paramString, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType, GeneratedKeyHolder paramGeneratedKeyHolder)
    throws java.sql.SQLException
  {
    SQLiteStatement localSQLiteStatement = null;
    try
    {
      localSQLiteStatement = this.db.compileStatement(paramString);
      bindArgs(localSQLiteStatement, paramArrayOfObject, paramArrayOfFieldType);
      long l = localSQLiteStatement.executeInsert();
      if (paramGeneratedKeyHolder != null)
        paramGeneratedKeyHolder.addKey(Long.valueOf(l));
      logger.trace("{}: insert statement is compiled and executed, changed {}: {}", this, Integer.valueOf(1), paramString);
      return 1;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("inserting to database failed: " + paramString, localSQLException);
    }
    finally
    {
      if (localSQLiteStatement != null)
        localSQLiteStatement.close();
    }
  }

  public boolean isAutoCommit()
    throws java.sql.SQLException
  {
    try
    {
      boolean bool = this.db.inTransaction();
      logger.trace("{}: in transaction is {}", this, Boolean.valueOf(bool));
      return !bool;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("problems getting auto-commit from database", localSQLException);
    }
  }

  public boolean isAutoCommitSupported()
  {
    return true;
  }

  public boolean isClosed()
    throws java.sql.SQLException
  {
    try
    {
      boolean bool = this.db.isOpen();
      logger.trace("{}: db {} isOpen returned {}", this, this.db, Boolean.valueOf(bool));
      return !bool;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("problems detecting if the database is closed", localSQLException);
    }
  }

  public boolean isReadWrite()
  {
    return this.readWrite;
  }

  public boolean isTableExists(String paramString)
  {
    Cursor localCursor = this.db.rawQuery("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = '" + paramString + "'", null);
    try
    {
      if (localCursor.getCount() > 0);
      for (boolean bool = true; ; bool = false)
      {
        logger.trace("{}: isTableExists '{}' returned {}", this, paramString, Boolean.valueOf(bool));
        return bool;
      }
    }
    finally
    {
      localCursor.close();
    }
  }

  public long queryForLong(String paramString)
    throws java.sql.SQLException
  {
    SQLiteStatement localSQLiteStatement = null;
    try
    {
      localSQLiteStatement = this.db.compileStatement(paramString);
      long l = localSQLiteStatement.simpleQueryForLong();
      logger.trace("{}: query for long simple query returned {}: {}", this, Long.valueOf(l), paramString);
      return l;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("queryForLong from database failed: " + paramString, localSQLException);
    }
    finally
    {
      if (localSQLiteStatement != null)
        localSQLiteStatement.close();
    }
  }

  public long queryForLong(String paramString, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType)
    throws java.sql.SQLException
  {
    Cursor localCursor = null;
    try
    {
      localCursor = this.db.rawQuery(paramString, toStrings(paramArrayOfObject));
      AndroidDatabaseResults localAndroidDatabaseResults = new AndroidDatabaseResults(localCursor, null);
      if (localAndroidDatabaseResults.first());
      for (long l = localAndroidDatabaseResults.getLong(0); ; l = 0L)
      {
        logger.trace("{}: query for long raw query returned {}: {}", this, Long.valueOf(l), paramString);
        return l;
      }
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("queryForLong from database failed: " + paramString, localSQLException);
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public <T> Object queryForOne(String paramString, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType, GenericRowMapper<T> paramGenericRowMapper, ObjectCache paramObjectCache)
    throws java.sql.SQLException
  {
    Cursor localCursor = null;
    try
    {
      localCursor = this.db.rawQuery(paramString, toStrings(paramArrayOfObject));
      AndroidDatabaseResults localAndroidDatabaseResults = new AndroidDatabaseResults(localCursor, paramObjectCache);
      logger.trace("{}: queried for one result: {}", this, paramString);
      boolean bool = localAndroidDatabaseResults.first();
      if (!bool)
      {
        localObject2 = null;
        return localObject2;
      }
      Object localObject2 = paramGenericRowMapper.mapRow(localAndroidDatabaseResults);
      if (localAndroidDatabaseResults.next())
      {
        localObject2 = MORE_THAN_ONE;
        return localObject2;
      }
      return localObject2;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("queryForOne from database failed: " + paramString, localSQLException);
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public void rollback(Savepoint paramSavepoint)
    throws java.sql.SQLException
  {
    try
    {
      this.db.endTransaction();
      if (paramSavepoint == null)
      {
        logger.trace("{}: transaction is ended, unsuccessfuly", this);
        return;
      }
      logger.trace("{}: transaction {} is ended, unsuccessfuly", this, paramSavepoint.getSavepointName());
      return;
    }
    catch (android.database.SQLException localSQLException)
    {
      if (paramSavepoint == null)
        throw SqlExceptionUtil.create("problems rolling back transaction", localSQLException);
      throw SqlExceptionUtil.create("problems rolling back transaction " + paramSavepoint.getSavepointName(), localSQLException);
    }
  }

  public void setAutoCommit(boolean paramBoolean)
  {
    if (paramBoolean)
      if (this.db.inTransaction())
      {
        this.db.setTransactionSuccessful();
        this.db.endTransaction();
      }
    while (this.db.inTransaction())
      return;
    this.db.beginTransaction();
  }

  public Savepoint setSavePoint(String paramString)
    throws java.sql.SQLException
  {
    try
    {
      this.db.beginTransaction();
      logger.trace("{}: save-point set with name {}", this, paramString);
      OurSavePoint localOurSavePoint = new OurSavePoint(paramString);
      return localOurSavePoint;
    }
    catch (android.database.SQLException localSQLException)
    {
      throw SqlExceptionUtil.create("problems beginning transaction " + paramString, localSQLException);
    }
  }

  public String toString()
  {
    return getClass().getSimpleName() + "@" + Integer.toHexString(super.hashCode());
  }

  public int update(String paramString, Object[] paramArrayOfObject, FieldType[] paramArrayOfFieldType)
    throws java.sql.SQLException
  {
    return update(paramString, paramArrayOfObject, paramArrayOfFieldType, "updated");
  }

  private static class OurSavePoint
    implements Savepoint
  {
    private String name;

    public OurSavePoint(String paramString)
    {
      this.name = paramString;
    }

    public int getSavepointId()
    {
      return 0;
    }

    public String getSavepointName()
    {
      return this.name;
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.j256.ormlite.android.AndroidDatabaseConnection
 * JD-Core Version:    0.6.2
 */